﻿Imports System.Data.SqlClient
Public Class FrmTimKiemvaBaoCaoBoDiaChinhCapTinh

    Public m_tennhap As String
    Private Sub NapDMHienTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMHienTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHienTrang.DisplayMember = "TenHienTrang"
        ComboBoxHienTrang.ValueMember = "MaHienTrang"
        ComboBoxHienTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiTaiLieu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTaiLieu", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxLoaiTaiLieu.DisplayMember = "TenLoaiTaiLieu"
        ComboBoxLoaiTaiLieu.ValueMember = "maLoaiTaiLieuID"
        ComboBoxLoaiTaiLieu.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiSoBo()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMSoBo", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComBoBoxTenLoaiSo.DisplayMember = "TenSoBo"
        ComBoBoxTenLoaiSo.ValueMember = "maSoBo"
        ComBoBoxTenLoaiSo.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewSoBoDiaChinh()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select " &
           " Maso" &
           ", NgayNhap" &
           ", Quyenso" &
           ", NamLap" &
           ", Ghichu" &
           ", CoSoPhapLy" &
           ", th.T_huyen" &
           ", tx.TenXa" &
           ", k.TenKho" &
           ", Ke.TenKe" &
           ", ht.TenHienTrang" &
           ", tli.TenLoaiTaiLieu" &
           ", sb.tensobo " &
           " From  SoBoDiaChinh dc,  DMHienTrang ht, DMTaiLieu tli, DMKho k, DMKe ke, tenxa tx, tenhuyen th, DMSoBo sb " &
                                " WHERE " &
                                    " dc.MaLoaiTaiLieuID = tli.maloaiTaiLieuID " &
                                    " and dc.maloaiTaiLieuId = '" & ComboBoxLoaiTaiLieu.SelectedValue.ToString & "' " &
                                    " and dc.makhoid = k.makhoid " &
                                    " and dc.maKeid = ke.makeid " &
                                    " and dc.mahuyenid = th.maH " &
                                    " and dc.maxaid = tx.maxa " &
                                    " and dc.maLoaiSoboID = sb.masobo " &
                                    " and dc.maLoaisoboId = '" & ComBoBoxTenLoaiSo.SelectedValue.ToString & "' " &
                                    " and dc.maHienTrangID = ht.mahienTrang " &
                                    " and dc.mahientrangid = '" & ComboBoxHienTrang.SelectedValue.ToString & "' " &
                                    " and dc.quyenso like '%" & TextBoxQuyenSo.Text & "%'" &
                                    " and dc.Ghichu like N'%" & TextBoxGhiChu.Text & "%' " &
                                    " and dc.NamLap like '%" & TextBoxNamLap.Text & "%' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewSoBoDiaChinh.DataSource = tbl
        If (tbl.Rows.Count = 0) Then
            MessageBox.Show("Không tìm thấy thông tin !")
        Else
        End If
        ob_cnn.Close()
    End Sub
    Private Sub FrmTimKiemSoBoDiaChinh_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' to mau cac dong trong datagridlietkebando doi mau khac nhau
        DataGridViewSoBoDiaChinh.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewSoBoDiaChinh.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        NapDMHienTrang()
        NapLoaiTaiLieu()
        NapLoaiSoBo()
    End Sub
    Private Sub ButtonTimKiem_Click(sender As Object, e As EventArgs) Handles ButtonTimKiem.Click
        NapDataGridViewSoBoDiaChinh()
    End Sub
    Private Sub TextBoxGhiChu_TextChanged(sender As Object, e As EventArgs) Handles TextBoxGhiChu.TextChanged
        NapDataGridViewSoBoDiaChinh()
    End Sub
    Private Sub ButtonThoát_Click(sender As Object, e As EventArgs) Handles ButtonThoát.Click
        Me.Close()
    End Sub
    Private Sub XuatExcel()
        ' xuat truc tiep ra excel
        Dim gr As SaveFileDialog = New SaveFileDialog()
        Dim FileName As String
        With gr
            .InitialDirectory = "D:\"
            .Filter = "Microsoft Excel Worksheet (*.xls)|*.xls"
            .FilterIndex = 1
        End With
        If gr.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            FileName = gr.FileName
        Else
            Exit Sub
        End If
        'Extracting from database
        Dim col, row As Integer
        Dim Excel As Object = CreateObject("Excel.Application")

        If Excel Is Nothing Then
            MsgBox("m***cel re..", MsgBoxStyle.Critical)
            Return
        End If
        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()
                .Worksheets(1).name = "So bo dia chinh "
                Dim i As Integer = 1
                For col = 0 To DataGridViewSoBoDiaChinh.Columns.Count - 1
                    .cells(1, i).value = DataGridViewSoBoDiaChinh.Columns(col).HeaderText
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To DataGridViewSoBoDiaChinh.Columns.Count - 1
                    i = 2
                    For row = 0 To DataGridViewSoBoDiaChinh.Rows.Count - 1
                        With .worksheets(1)
                            .Cells(i, k).Value = DataGridViewSoBoDiaChinh.Rows(row).Cells(col).Value
                            i += 1
                        End With
                    Next
                    k += 1
                Next
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MsgBox("Dữ liệu xuất ra Excel thành công!", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub
    Private Sub ButtonExcel_Click(sender As Object, e As EventArgs) Handles ButtonExcel.Click
        XuatExcel()
    End Sub
    Private Sub TextBoxNamLap_TextChanged(sender As Object, e As EventArgs) Handles TextBoxNamLap.TextChanged
        NapDataGridViewSoBoDiaChinh()
    End Sub

    Private Sub DataGridViewSoBoDiaChinh_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewSoBoDiaChinh.CellContentClick

    End Sub

    Private Sub DataGridViewSoBoDiaChinh_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewSoBoDiaChinh.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewSoBoDiaChinh.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewSoBoDiaChinh.RowHeadersWidth < Size.Width + 20 Then DataGridViewSoBoDiaChinh.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
End Class